﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XbsWebAPI.Application;
using XbsWebAPI.Application.SystemAdmin.Dtos;
using XbsWebAPI.Core.Tools;
using XbsWebAPI.EntityFrameworkCore.Entities;
using XbsWebAPI.EntityFrameworkCore.Entities.SystemAdmin;

namespace XbsWebAPI.Core.Repositories
{
    /// <summary>
    /// 菜单 应用服务层
    /// </summary>
    public class MenuAppService : IMenuAppService
    {
        private readonly IMenuRepository _menuRepository;
        private readonly ILogger<MenuAppService> _logger;
        private readonly IMapper _mapper;

        public MenuAppService(IMenuRepository menuRepository,ILogger<MenuAppService> logger,IMapper mapper)
        {
            _menuRepository = menuRepository;
            _logger = logger;
            _mapper = mapper;
        }

        public Task<ResultDto<string>> AddMenusAsync(Menu model)
        {
            var result = new ResultDto<string>();
            try
            {
                _menuRepository.CreateAsync(model);
                result.Message = "操作成功";
            }
            catch (Exception e)
            {
                result.Code = -1;
                result.Message = e.Message;
                return Task.FromResult(result);
            }

            return Task.FromResult(result);
        }

        public ResultDto<List<ListMenu>> CompositionMenus()
        {
            var result = new ResultDto<List<ListMenu>>();

            try
            {
                var listMenus = new List<ListMenu>();
                var menus = _menuRepository.AsQueryable().ToList();
                //一级菜单
                var pMenus = menus.Where(a => a.PId == null || a.PId == Guid.Empty).OrderBy(a => a.Sort).ToList();
                foreach (var pMenu in pMenus)
                {
                    var listMenu = new ListMenu();
                    listMenu.Menu = pMenu;
                    listMenu.ChilrenMenus = menus.Where(a => a.PId == pMenu.Id).OrderBy(a => a.Sort).ToList();
                    listMenus.Add(listMenu);
                }
                result.Result = listMenus;
                result.Message = "操作成功";
            }
            catch (Exception e)
            {
                result.Code = -1;
                result.Message = e.Message;
                _logger.LogError("CompositionMenus:" + e.Message);
                return result;
            }

            return result;
        }

        public Task<ResultDto<string>> CreateMenuAsync(CreateMenuDto model)
        {
            var result = new ResultDto<string>();
            try
            {
                var entity = _mapper.Map<Menu>(model);
                _menuRepository.CreateAsync(entity);
                result.Message = "操作成功";
            }
            catch (Exception e)
            {
                _logger.LogError("CreateMenuAsync:" + e.Message);
                result.Code = -1;
                result.Message = e.Message;
                return Task.FromResult(result);
            }

            return Task.FromResult(result);
        }

        public ResultDto<List<Menu>> GetParentMenus()
        {
            var result = new ResultDto<List<Menu>>();
            try
            {
                var menus = _menuRepository.Query(a => a.MenuType == MenuType.Parent).ToList();
                result.Message = "操作成功";
                result.Result = menus;
            }
            catch (Exception e)
            {
                _logger.LogError("GetParentMenus:" + e.Message);
                result.Code = -1;
                result.Message = e.Message;
                return result;
            }

            return result;
        }

        public ResultDto<List<Menu>> GetButonMenus(Guid childMenuId)
        {
            var result = new ResultDto<List<Menu>>();
            try
            {
                var menus = _menuRepository.Query(a => a.PId == childMenuId).ToList();
                result.Message = "操作成功";
                result.Result = menus;
            }
            catch (Exception e)
            {
                _logger.LogError("GetButonMenus:" + e.Message);
                result.Code = -1;
                result.Message = e.Message;
                return result;
            }

            return result;
        }

        public ResultDto<List<Menu>> GetChildMenus(Guid parentMenuId)
        {
            var result = new ResultDto<List<Menu>>();
            try
            {
                var menus = _menuRepository.Query(a => a.PId == parentMenuId).ToList();
                result.Message = "操作成功";
                result.Result = menus;
            }
            catch (Exception e)
            {
                _logger.LogError("GetButonMenus:" + e.Message);
                result.Code = -1;
                result.Message = e.Message;
                return result;
            }

            return result;
        }

        public ResultDto<Menu> GetMenuById(Guid id)
        {
            var result = new ResultDto<Menu>();
            try
            {
                var model = _menuRepository.QueryById(id);
                if (model == null)
                {
                    result.Code = -1;
                    var msg = $"Id={id}的菜单不存在";
                    result.Message = msg;
                    _logger.LogError("GetMenuById:" + msg);
                    return result;
                }
                result.Result = model;
            }
            catch (Exception e)
            {
                result.Code = -1;
                var msg = $"Id={id}的菜单异常，{e.Message}";
                result.Message = msg;
                _logger.LogError("GetMenuById:" + e.Message);
                return result;
            }

            return result;
        }

        public ResultDto<List<Menu>> GetMenus(MenuType? menuType)
        {
            var result =  new ResultDto<List<Menu>>();
            try
            {
                var query = _menuRepository.AsQueryable();

                if(menuType != null)
                {
                    query = query.Where(a => a.MenuType == menuType);
                }

                result.Result = query.ToList();
                result.Message = "操作成功";
            }
            catch (Exception e)
            {
                result.Message = e.Message;
                _logger.LogError("GetMenus:" + e.Message);
                result.Code= -1;
                return result;
            }

            return result;
        }

        public Task<ResultDto<string>> RemoveMenuByIdAsync(Guid id)
        {
            var result = new ResultDto<string>();
            try
            {
                _menuRepository.DeleteAsync(id);
                result.Message = "操作成功";
            }
            catch (Exception e)
            {
                _logger.LogError("RemoveMenuByIdAsync:" + e.Message);
                result.Code = -1;
                result.Message = e.Message;
                return Task.FromResult(result);
            }

            return Task.FromResult(result);
        }

        public Task<ResultDto<string>> UpdateMenuAsync(EditMenuDto model)
        {
            var result = new ResultDto<string>();
            try
            {
                var entity = _mapper.Map<Menu>(model);
                _menuRepository.UpdateAsync(entity);
                result.Message = "操作成功";
            }
            catch (Exception e)
            {
                _logger.LogError("UpdateMenuAsync:" + e.Message);
                result.Code = -1;
                result.Message = e.Message;
                return Task.FromResult(result);
            }

            return Task.FromResult(result);
        }
    }
}
